package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page<CheckItem> page = null;

        //构造条件 Wrapper对象。添加上where is_delete=0的条件
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);

        //apache 字符串增强工具类。isBlank是不是为空
        if(StringUtils.isBlank(queryPageBean.getQueryString())){
            //无条件查询
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        }else {
            //column数据库字段名
            queryWrapper.like("name",queryPageBean.getQueryString());
            //条件查询
            page = page(new Page<CheckItem>(queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize()),queryWrapper);
        }
        PageResult  result = new PageResult(page.getTotal(),page.getRecords());
        return result;
    }

    //逻辑删除
    @Override
    public Boolean updateCheckitemById(int id) {
        //创建检查项对象，并设置id
        CheckItem checkItem = new CheckItem();
        checkItem.setId(id);
        //修改is_delete字段为1,是他变为无效
        checkItem.setIs_delete(1);
//        逻辑删除的原理：就是查询的时候加上where is_delete=0
//        的条件，凡是不等于0的都查询不出来(不显示)，
//        即视为逻辑删除(数据失效,数据库还存在)，
//        所以但凡查询都要加上where is_delete=0的条件
        return updateById(checkItem);
    }
}
